﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerPhongBanDao : IPhongBanDao
    {
        public SqlServerPhongBanDao()
        {
            SQLUtility.TableName = "PhongBan";
        }

        //Trả về đối tượng báo ăn
        private PhongBan MakePhongBan(DataRow row)
        {
            Guid _id = Guid.Parse(row["PhongBanID"].ToString());
            string _soPhongBan = row["SoPhongBan"].ToString();
            string _tenPhongBan = row["TenPhongBan"].ToString();
            int _schoolID = int.Parse(row["SchoolID"].ToString());

         return new PhongBan(_id, _soPhongBan, _tenPhongBan, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<PhongBan> MakePhongBans(DataTable dt)  
        {
            IList<PhongBan> list = new List<PhongBan>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakePhongBan(row));
            }
            return list;
        }
        private DbParameter[] Take(PhongBan PhongBan)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@PhongBanID", PhongBan.Id);
            parameters[1] = new SqlParameter("@SoPhongBan", PhongBan.SoPhongBan);
            parameters[2] = new SqlParameter("@TenPhongBan", PhongBan.TenPhongBan);

            parameters[3] = new SqlParameter("@SchoolID", PhongBan.SchoolID);
 

            return parameters;
        }

        public IList<Business_Objects.PhongBan> GetPhongBan()
        {
            SQLUtility.TableName = "PhongBan";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakePhongBans(dt);
        }

        public Business_Objects.PhongBan GetPhongBanByID(string PhongBanID)
        {
            SQLUtility.TableName = "PhongBan";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@PhongBanID", PhongBanID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakePhongBan(row);
            }
        }

        public void InsertPhongBan(Business_Objects.PhongBan PhongBan)
        {
            SQLUtility.TableName = "PhongBan";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(PhongBan), true);
        }

        public int UpdatePhongBan(Business_Objects.PhongBan PhongBan)
        {
            SQLUtility.TableName = "PhongBan";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(PhongBan)); }
            catch { return -1; }
        }

        public int DeletePhongBan(Business_Objects.PhongBan PhongBan)
        {
            SQLUtility.TableName = "PhongBan";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(PhongBan)); }
            catch { return -1; }
        }
    }
}
